package com.weavernorth.action.application;

import com.weaver.general.BaseBean;
import com.weavernorth.util.Logger4jUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.List;

import static com.weavernorth.util.dateUtils.dateCompare;
import static com.weavernorth.util.dateUtils.dateTimeBetween;

/**
 * @Author: bruseewang
 * @Package: com.weavernorth.action.application
 * @date: 2020/7/29 18:26
 * @description: 外勤加班开始时间和结束时间与加班申请流程表时间区间校验
 */
public class CheckDate_wqjb implements Action {
    public static org.apache.log4j.Logger log = Logger4jUtil.getLoggerByName("/action/application/CheckDate_wqjb/");
    @Override
    public String execute(RequestInfo requestInfo) {
        log.info("外勤加班流程时间区间校验开始：");
        RecordSet rsTable=new RecordSet();
        RecordSet rsDetail=new RecordSet();
        //获取请求ID
        String strLcId = requestInfo.getRequestid();
        log.info("requestid的值是：" + strLcId);

        String strTableName ="";
        String strSqlMain="select tablename  from workflow_bill where id = (select formid from workflow_base where id=(select workflowid from workflow_requestbase  where requestid='"+strLcId+"') and isbill=1)";
        log.info("查询主表数据的sql：" + strSqlMain);
        //查询主表数据的sql
        rsTable.execute(strSqlMain);
        if(rsTable.next()){
            strTableName=rsTable.getString("tablename");
        }

        log.info("tablename的值是：" + strTableName);
        String strBdId = requestInfo.getWorkflowid();
        log.info("workflowid的值是：" + strBdId);
        //查询明细表数据的sql
        String strSqlDetail = "select hr.lastname,fmd.* from " + strTableName + " fm," + strTableName + "_dt1 fmd,hrmresource hr  where fmd.mainid=fm.id AND hr.id = fmd.detail_resourceId and  fm.requestid = '" + strLcId + "'";
        log.info("查询明细表数据的sql：" + strSqlDetail);
        rsDetail.executeQuery(strSqlDetail);
        List<String> list=new ArrayList<String>();
        while (rsDetail.next()){
            String strUser= Util.null2String(rsDetail.getString("detail_resourceId"));

            String strLastName=Util.null2String(rsDetail.getString("lastname"));
            //开始日期
            String strBeginDate= Util.null2String(rsDetail.getString("detail_fromDate"));
            //结束日期
            String strEndDate= Util.null2String(rsDetail.getString("detail_toDate"));
            //开始时间
            String strBeginTime= Util.null2String(rsDetail.getString("detail_fromTime"));
            //结束时间
            String strEndTime= Util.null2String(rsDetail.getString("detail_toTime"));
            String strBegin=strBeginDate+" "+strBeginTime;
            String strEnd=strEndDate+" "+strEndTime;
            list.add(strUser+","+strLastName+","+strBegin+","+strEnd);
        }
        log.info("获取表单数据："+list.toString());
        String strCode="";
        String strMess="";
        String strResult;
        try {
            strResult=checkDate(list);
            strCode=strResult.split(",")[0].toString();
            strMess=strResult.split(",")[1];
        } catch (Exception e) {
            e.printStackTrace();
            log.info("日期对比出现异常："+e.toString());
            requestInfo.getRequestManager().setMessagecontent("日期对比出现异常："+e.toString());
            return FAILURE_AND_CONTINUE;
        }
        //如果存在时间交叉,则不允许提交
        if (strCode.equals("1")){
            log.info(strMess);
            requestInfo.getRequestManager().setMessagecontent(strMess);
            return FAILURE_AND_CONTINUE;
        }else if (strCode.equals("2")){
            log.info("程序异常,异常消息："+strMess);
            requestInfo.getRequestManager().setMessagecontent("程序异常,异常消息："+strMess);
            return FAILURE_AND_CONTINUE;
        }
        return SUCCESS;
    }
    public static String checkDate(List<String> list) {
        String result="0,无交叉日期数据";
        BaseBean bb=new BaseBean();
        if (list.size()>0){
            for (String s : list) {
                String usrid=s.split(",")[0];
                String usrName=s.split(",")[1];
                String beginTime=s.split(",")[2];
                String begin="";
                if(beginTime.length()>10){
                    begin=beginTime.substring(0,10);
                }
                String end=s.split(",")[3];
                String strTable = Util.null2String(bb.getPropValue("attendMess", "wcsqTable"));
                RecordSet rswqjb=new RecordSet();
                //获取某个人所有的加班申请数据
                String strSql = "select fmd.* from " + strTable + " fm," + strTable + "_dt1 fmd,workflow_requestbase wr where fm.id=fmd.mainid and fmd.detail_resourceId="+usrid+" and wr.requestid=fm.requestid and wr.currentnodetype!='0' and fmd.detail_fromDate>='"+begin+"'";
                log.info("查询加班申请数据的sql:"+strSql);
                try {
                    rswqjb.executeQuery(strSql);
                    while (rswqjb.next()) {
                        String strBeginDate = Util.null2String(rswqjb.getString("detail_fromDate"));
                        String strBeginTime = Util.null2String(rswqjb.getString("detail_fromTime"));
                        String strEndDate = Util.null2String(rswqjb.getString("detail_toDate"));
                        String strEndTime = Util.null2String(rswqjb.getString("detail_toTime"));
                        String starTime = strBeginDate + " " + strBeginTime;
                        String endTime = strEndDate + " " + strEndTime;
                        //当外勤加班的开始日期比加班申请开始日期小，流程区间的结束日期比加班申请的结束日期小时
                        log.info("加班申请开始日期："+starTime+";加班申请结束日期："+endTime);
                        String strMes="";
                        if (dateCompare(starTime,beginTime)&&dateCompare(end,endTime))
                        {
                            strMes="员工"+usrName+"在加班申请中已存在重复时间段，如当前外勤加班申请日期区间：" + begin + "————" + end + ";加班日期区间：" + starTime + "————" + endTime;
                            log.info(strMes);
                            result = "1," + usrName+","+strMes;
                            break;
                        }
                        //1.将加班申请开始日期与流程日期区间对比,如果存在交叉则返回false
                        if (dateTimeBetween(starTime, beginTime, end)) {
                            strMes="员工"+usrName+"在加班申请中已存在重复时间段，如当前外勤加班开始日期：" + begin + ";加班申请日期区间：" + starTime + "————" + endTime;
                            log.info(strMes);
                            result = "1," + usrName+","+strMes;
                            break;
                        } else {
                            //2.将加班申请结束日期与程日期区间对比,如果存在交叉则返回false
                            if (dateTimeBetween(endTime, beginTime, end)) {
                                strMes="员工"+usrName+"在加班申请中已存在重复时间段，如当前外勤加班结束日期：" + end + ";加班申请日期区间：" + starTime + "————" + endTime;
                                log.info(strMes);
                                result = "1," + usrName+","+strMes;
                                break;
                            }

                        }
                    }
                }catch (Exception ex){
                    result = "2," + ex.toString();
                    log.info(result);
                }
            }
        }
        return result;
    }
}
